﻿using System.Net;
using System.Text;

namespace CapturePackageWebApi.Model
{
    public class HttpHelper
    {
        /// <summary>
        /// 根据url下载内容  之前是GB2312
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string DownloadUrl(string url)
        {
            return DownloadHtml(url, Encoding.UTF8);
        }

        //HttpClient--WebApi

        /// <summary>
        /// 下载html
        /// http://tool.sufeinet.com/HttpHelper.aspx
        /// HttpWebRequest功能比较丰富，WebClient使用比较简单
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string DownloadHtml(string url, Encoding encode)
        {
            string html = string.Empty;
            try
            {
                //https可以下载--
                //设置https等级
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) =>
                //{
                //    return true; //总是接受  
                //});
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;//模拟请求
                request.Timeout = 30 * 1000;//设置30s的超时

                //设置用户请求的身份 设置的为浏览器
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36";
                //request.UserAgent = "User - Agent:Mozilla / 5.0(iPhone; CPU iPhone OS 7_1_2 like Mac OS X) App leWebKit/ 537.51.2(KHTML, like Gecko) Version / 7.0 Mobile / 11D257 Safari / 9537.53";

                request.ContentType = "text/html; charset=utf-8";// "text/html;charset=gbk";// 
                                                                 //request.Host = "search.yhd.com";

                request.Headers.Add("Cookie", @"shshshfpa=5fa7024b-c448-bfe9-9d69-62f7fee531ca-1711935463; shshshfpx=5fa7024b-c448-bfe9-9d69-62f7fee531ca-1711935463; __jdu=17119354643752096615671; o2State={%22webp%22:true%2C%22avif%22:true}; pinId=Chypgm-4IAe2int-M6lAv7V9-x-f3wj7; unpl=JF8EAMlnNSttCkMBUh9QGRoSHFpcW10NTUdQOjNSAA9eHFdXTAZPFEd7XlVdWBRKHx9sYxRUWFNPUA4aAisSEXteXVdZDEsWC2tXVgQFDQ8VXURJQlZAFDNVCV9dSRZRZjJWBFtdT1xWSAYYRRMfDlAKDlhCR1FpMjVkXlh7VAQrAh0SEENfUFtaDUkRB2xmDVNaWE9TAxsyGiIXe21kVlsJSx4zblcEZB8MF1cFHwEaGl1LW1ReVQpPEgRqZQNQXllDUwIbBhwUEHtcZF0; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_c9df5c393f694147abdefec7f2cf5e7f|1727247301804; 3AB9D23F7A4B3CSS=jdd03BZKDBKCV6CYRWGBSQAE5AIRIBHJ4BBGTPZWOYSOSSCLSGNGSWQW6DTMIRSB2XO76V5JVPMW4F5ZC425SN5PGMGYZ6MAAAAMSE73D2AAAAAAAD4UT4IN55DXROQX; _gia_d=1; areaId=15; ipLoc-djd=15-1262-0-0; PCSYCityID=CN_330000_330100_0; TrackID=1cnXToqXEIPP4tnyXfQitfVNhcXMa7JTJbe98x8CWg4_A0OH3xnB-Kd4ZK2EYLrrjPBwMMfOJIVRi48pL7Pfs-o4p1w7l9kt7_-rzDMYBEFk; thor=B366F669033BCC42C96A20BDF510AED4E1F854A06C637D038E879929C3657223E2BFB34299DE9AB759E1A3BB66C55601DA124DEA1A585DE817499953D16F822A0EAC367AE82311064FF501DC064745EA7F91433BFB69F62AD0E4DCCBF2C392F0DD8115C14B5D8AB946EF170F7488198B0D814B046ED617FCEA217A31ED2BB18FEF0C103184FC27BC3A696DE6ECD52E3E4DDBD5C0F3B4555FB284596601C223F7; light_key=AASBKE7rOxgWQziEhC_QY6ya69jun_DD8hvT5WMf8N6d4bALBL-C2HUg7Ur1lPUvc70RW5jC; pin=jd_7d3ce85b4236d; unick=%E5%A5%8B%E6%96%97%E5%90%A7%E4%B8%8D%E8%B4%9F%E9%9D%92%E6%98%A5%E5%B9%B4%E5%8D%8E; ceshi3.com=201; _tp=2q8D1Ml8Erk9SGqjBcerL3xUG%2FEUtSXFQG0f3EIJNcw%3D; _pst=jd_7d3ce85b4236d; cn=109; 3AB9D23F7A4B3C9B=BZKDBKCV6CYRWGBSQAE5AIRIBHJ4BBGTPZWOYSOSSCLSGNGSWQW6DTMIRSB2XO76V5JVPMW4F5ZC425SN5PGMGYZ6M; flash=3_fVJKByLacYl1mUlsgxEshtFmMsOuuME6Bu_8SkVWqGAQNn8FsLuEF9rGkuHYi56whyEgVhRMW0Ic_E-hudGrH29oXGp8QmiNuvMcH5irHbrg4pg7IWLvJj7wESDZ9L9vI3N6C8TgIgvNfAHRpIoFxNxA5dDXesRAZlaEC2zUlxgtN6AKfRFzWk**; __jda=76161171.17119354643752096615671.1711935464.1725873969.1727247302.11; __jdb=76161171.6.17119354643752096615671|11.1727247302; __jdc=76161171; UseCorpPin=jd_7d3ce85b4236d; shshshfpb=BApXSqbf_JPdAUaxbNg9RXS-gC4BFI4AQBlPSU0xg9xJ1MlQ0F4C2");

                //request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                //request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
                //request.Headers.Add("Referer", "http://list.yhd.com/c0-0/b/a-s1-v0-p1-price-d0-f0-m1-rt0-pid-mid0-kiphone/");

                //Encoding enc = Encoding.GetEncoding("GB2312"); // 如果是乱码就改成 utf-8 / GB2312

                //如何自动读取cookie
                request.CookieContainer = new CookieContainer();//1 给请求准备个container
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)//发起请求
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                    }
                    else
                    {
                        try
                        {
                            //string sessionValue = response.Cookies["ASP.NET_SessionId"].Value;//2 读取cookie
                            StreamReader sr = new StreamReader(response.GetResponseStream(), encode);
                            html = sr.ReadToEnd();//读取数据
                            sr.Close();
                        }
                        catch (Exception ex)
                        {
                            html = null;
                        }
                    }
                }
            }
            catch (System.Net.WebException ex)
            {
                if (ex.Message.Equals("远程服务器返回错误: (306)。"))
                {
                    html = null;
                }
            }
            catch (Exception ex)
            {
                html = null;
            }
            return html;
        }

        /// <summary>
        /// 下载html
        /// http://tool.sufeinet.com/HttpHelper.aspx
        /// HttpWebRequest功能比较丰富，WebClient使用比较简单
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string DownloadHtmlModel(HerderModel herderModel, Encoding encode)
        {
            string html = string.Empty;
            try
            {
                //https可以下载--
                //设置https等级
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) =>
                //{
                //    return true; //总是接受  
                //});
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                HttpWebRequest request = HttpWebRequest.Create(herderModel.url) as HttpWebRequest;//模拟请求
                request.Timeout = 30 * 1000;//设置30s的超时

                //设置用户请求的身份 设置的为浏览器
                //request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36";
                ////request.UserAgent = "User - Agent:Mozilla / 5.0(iPhone; CPU iPhone OS 7_1_2 like Mac OS X) App leWebKit/ 537.51.2(KHTML, like Gecko) Version / 7.0 Mobile / 11D257 Safari / 9537.53";

                //request.ContentType = "text/html; charset=utf-8";// "text/html;charset=gbk";// 
                //                                                 //request.Host = "search.yhd.com";

                //request.Headers.Add("Cookie", @"shshshfpa=5fa7024b-c448-bfe9-9d69-62f7fee531ca-1711935463; shshshfpx=5fa7024b-c448-bfe9-9d69-62f7fee531ca-1711935463; __jdu=17119354643752096615671; o2State={%22webp%22:true%2C%22avif%22:true}; pinId=Chypgm-4IAe2int-M6lAv7V9-x-f3wj7; unpl=JF8EAMlnNSttCkMBUh9QGRoSHFpcW10NTUdQOjNSAA9eHFdXTAZPFEd7XlVdWBRKHx9sYxRUWFNPUA4aAisSEXteXVdZDEsWC2tXVgQFDQ8VXURJQlZAFDNVCV9dSRZRZjJWBFtdT1xWSAYYRRMfDlAKDlhCR1FpMjVkXlh7VAQrAh0SEENfUFtaDUkRB2xmDVNaWE9TAxsyGiIXe21kVlsJSx4zblcEZB8MF1cFHwEaGl1LW1ReVQpPEgRqZQNQXllDUwIbBhwUEHtcZF0; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_c9df5c393f694147abdefec7f2cf5e7f|1727247301804; 3AB9D23F7A4B3CSS=jdd03BZKDBKCV6CYRWGBSQAE5AIRIBHJ4BBGTPZWOYSOSSCLSGNGSWQW6DTMIRSB2XO76V5JVPMW4F5ZC425SN5PGMGYZ6MAAAAMSE73D2AAAAAAAD4UT4IN55DXROQX; _gia_d=1; areaId=15; ipLoc-djd=15-1262-0-0; PCSYCityID=CN_330000_330100_0; TrackID=1cnXToqXEIPP4tnyXfQitfVNhcXMa7JTJbe98x8CWg4_A0OH3xnB-Kd4ZK2EYLrrjPBwMMfOJIVRi48pL7Pfs-o4p1w7l9kt7_-rzDMYBEFk; thor=B366F669033BCC42C96A20BDF510AED4E1F854A06C637D038E879929C3657223E2BFB34299DE9AB759E1A3BB66C55601DA124DEA1A585DE817499953D16F822A0EAC367AE82311064FF501DC064745EA7F91433BFB69F62AD0E4DCCBF2C392F0DD8115C14B5D8AB946EF170F7488198B0D814B046ED617FCEA217A31ED2BB18FEF0C103184FC27BC3A696DE6ECD52E3E4DDBD5C0F3B4555FB284596601C223F7; light_key=AASBKE7rOxgWQziEhC_QY6ya69jun_DD8hvT5WMf8N6d4bALBL-C2HUg7Ur1lPUvc70RW5jC; pin=jd_7d3ce85b4236d; unick=%E5%A5%8B%E6%96%97%E5%90%A7%E4%B8%8D%E8%B4%9F%E9%9D%92%E6%98%A5%E5%B9%B4%E5%8D%8E; ceshi3.com=201; _tp=2q8D1Ml8Erk9SGqjBcerL3xUG%2FEUtSXFQG0f3EIJNcw%3D; _pst=jd_7d3ce85b4236d; cn=109; 3AB9D23F7A4B3C9B=BZKDBKCV6CYRWGBSQAE5AIRIBHJ4BBGTPZWOYSOSSCLSGNGSWQW6DTMIRSB2XO76V5JVPMW4F5ZC425SN5PGMGYZ6M; flash=3_fVJKByLacYl1mUlsgxEshtFmMsOuuME6Bu_8SkVWqGAQNn8FsLuEF9rGkuHYi56whyEgVhRMW0Ic_E-hudGrH29oXGp8QmiNuvMcH5irHbrg4pg7IWLvJj7wESDZ9L9vI3N6C8TgIgvNfAHRpIoFxNxA5dDXesRAZlaEC2zUlxgtN6AKfRFzWk**; __jda=76161171.17119354643752096615671.1711935464.1725873969.1727247302.11; __jdb=76161171.6.17119354643752096615671|11.1727247302; __jdc=76161171; UseCorpPin=jd_7d3ce85b4236d; shshshfpb=BApXSqbf_JPdAUaxbNg9RXS-gC4BFI4AQBlPSU0xg9xJ1MlQ0F4C2");
                request.UserAgent = herderModel.UserAgent;
                request.ContentType = herderModel.ContentType;
                request.Headers.Add("Cookie",herderModel.Cookie);
                //request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                //request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
                //request.Headers.Add("Referer", "http://list.yhd.com/c0-0/b/a-s1-v0-p1-price-d0-f0-m1-rt0-pid-mid0-kiphone/");

                //Encoding enc = Encoding.GetEncoding("GB2312"); // 如果是乱码就改成 utf-8 / GB2312

                //如何自动读取cookie
                request.CookieContainer = new CookieContainer();//1 给请求准备个container
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)//发起请求
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                    }
                    else
                    {
                        try
                        {
                            //string sessionValue = response.Cookies["ASP.NET_SessionId"].Value;//2 读取cookie
                            StreamReader sr = new StreamReader(response.GetResponseStream(), encode);
                            html = sr.ReadToEnd();//读取数据
                            sr.Close();
                        }
                        catch (Exception ex)
                        {
                            html = null;
                        }
                    }
                }
            }
            catch (System.Net.WebException ex)
            {
                if (ex.Message.Equals("远程服务器返回错误: (306)。"))
                {
                    html = null;
                }
            }
            catch (Exception ex)
            {
                html = null;
            }
            return html;
        }
    }
}
